package com.example.deertrip.ui.my;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.deertrip.Login_main_activity;
import com.example.deertrip.MainActivity;
import com.example.deertrip.OrderActivity;
import com.example.deertrip.R;
import com.example.deertrip.databinding.FragmentMyBinding;
import com.example.deertrip.tools_class.UserStateGet;


public class MyFragment extends Fragment {
   private Button btn_loginout;
   private Button btn_myorder;
   private TextView username;
    private TextView userphone;
    private TextView address;
    private TextView wechatmoney;
    private TextView applymoney;
    private TextView cardmoney;

    private FragmentMyBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {


        MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);

        binding = FragmentMyBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        btn_loginout=root.findViewById(R.id.my_loginout_button);
        btn_myorder=root.findViewById(R.id.my_order_button);
        username=root.findViewById(R.id.my_username);
        userphone=root.findViewById(R.id.my_userphonenumber);
        address=root.findViewById(R.id.my_useraddress);
        wechatmoney=root.findViewById(R.id.my_userwechatmoney);
        applymoney=root.findViewById(R.id.my_userapplymoney);
        cardmoney=root.findViewById(R.id.my_usercardmoney);

        UserStateGet userStateGet=new UserStateGet();
        userStateGet.vlidate(this.getActivity());
        getdata();
        listen();
        return root;
    }

    private void getdata() {
        SharedPreferences sp= getActivity().getSharedPreferences("deertrip",Context.MODE_PRIVATE);
        String name=sp.getString("UserName","鹿途新用户");
        String phone=sp.getString("UserTel","未录入信息");
        String dizhi=sp.getString("UserAddress","未录入信息");
        String wechat=sp.getString("WxBalance","0");
        String apply=sp.getString("ZfbBalance","0");
        String yinhangka=sp.getString("YhkBalance","0");

        username.setText(name);
        userphone.setText(phone);
        address.setText(dizhi);
        wechatmoney.setText(wechat);
        applymoney.setText(apply);
        cardmoney.setText(yinhangka);

    }

    private void listen() {
        btn_myorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), OrderActivity.class);
                startActivity(intent);
            }
        });

        btn_loginout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserStateGet userStateGet=new UserStateGet();
                userStateGet.login_out(getActivity());
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}